package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysCertificateInfo;
import com.ruoyi.system.service.ISysCertificateInfoService;
import com.ruoyi.system.util.DateUtils;
import com.ruoyi.system.util.LicenseGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * 证书管理Controller
 *
 * @author ruoyi
 * @date 2023-10-18
 */
@RestController
@RequestMapping("/certificate")
public class SysCertificateInfoController extends BaseController {
    @Autowired
    private ISysCertificateInfoService sysCertificateInfoService;

    /**
     * 查询证书管理列表
     */
    @RequiresPermissions("system:certificate:list")
    @GetMapping("/list")
    public TableDataInfo list(SysCertificateInfo sysCertificateInfo) {
        startPage();
        List<SysCertificateInfo> list = sysCertificateInfoService.selectSysCertificateInfoList(sysCertificateInfo);
        return getDataTable(list);
    }

    /**
     * 导出证书管理列表
     */
    @RequiresPermissions("system:certificate:export")
    @Log(title = "证书管理", businessType = BusinessType.EXPORT)
    @PostMapping("/licenseDownload")
    public void export(HttpServletResponse response, SysCertificateInfo sysCertificateInfo) throws IOException {
        System.out.println(sysCertificateInfo);
        OutputStream out = response.getOutputStream();
        sysCertificateInfo = sysCertificateInfoService.selectSysCertificateInfoByCertificateId(sysCertificateInfo.getCertificateId());
        System.out.println(sysCertificateInfo);
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; fileName=license_" + sysCertificateInfo.getCertificateCheckAddress() + ".lic");
        byte[] content = LicenseGenerator.function(sysCertificateInfo);
        out.write(content);
        out.close();
        sysCertificateInfo.setCertificateCreateTime(new Date());
        sysCertificateInfoService.updateSysCertificateInfo(sysCertificateInfo);
    }

    /**
     * 获取证书管理详细信息
     */
    @RequiresPermissions("system:certificate:query")
    @GetMapping(value = "/{certificateId}")
    public AjaxResult getInfo(@PathVariable("certificateId") Long certificateId) {
        return success(sysCertificateInfoService.selectSysCertificateInfoByCertificateId(certificateId));
    }

    /**
     * 新增证书管理
     */
    @RequiresPermissions("system:certificate:add")
    @Log(title = "证书管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCertificateInfo sysCertificateInfo) {
        Date current = new Date();
        if (DateUtils.getMinusDay(current, 1).after(sysCertificateInfo.getCertificateDeadline())) {
            sysCertificateInfo.setCertificateCheckStatus("失效");
        } else {
            sysCertificateInfo.setCertificateCheckStatus("有效");
        }
        return toAjax(sysCertificateInfoService.insertSysCertificateInfo(sysCertificateInfo));
    }

    /**
     * 修改证书管理
     */
    @RequiresPermissions("system:certificate:edit")
    @Log(title = "证书管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCertificateInfo sysCertificateInfo) {
        return toAjax(sysCertificateInfoService.updateSysCertificateInfo(sysCertificateInfo));
    }

    /**
     * 删除证书管理
     */
    @RequiresPermissions("system:certificate:remove")
    @Log(title = "证书管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{certificateIds}")
    public AjaxResult remove(@PathVariable Long[] certificateIds) {
        return toAjax(sysCertificateInfoService.deleteSysCertificateInfoByCertificateIds(certificateIds));
    }
}
